package com.example.backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.domain.entity.RestBean;
import com.example.backend.domain.po.Appointments;
import com.example.backend.service.IAppointmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dyy
 * @since 2025-05-27
 */
@RestController
@RequestMapping("/appointments")
public class AppointmentsController {
    @Autowired
    private IAppointmentsService appointmentsService;

    // 根据 ID 查询单个预约
    @GetMapping("/{id}")
    public RestBean<Appointments> getAppointmentById(@PathVariable Integer id) {
        return RestBean.success(appointmentsService.getById(id));
    }


    // 根据多个 ID 查询预约
    @GetMapping("/batch")
    public RestBean<List<Appointments>> getAppointmentsByIds(@RequestParam List<Integer> ids) {
        return RestBean.success(appointmentsService.listByIds(ids));
    }

    // 根据条件查询预约
    @GetMapping("/search")
    public RestBean<List<Appointments>> searchAppointments(
            @RequestParam(required = false) Integer customerId,
            @RequestParam(required = false) Integer employeeId,
            @RequestParam(required = false) Integer serviceId,
            @RequestParam(required = false) String status) {
        LambdaQueryWrapper<Appointments> queryWrapper = new LambdaQueryWrapper<>();
        if (customerId != null) {
            queryWrapper.eq(Appointments::getCustomerId, customerId);
        }
        if (employeeId != null) {
            queryWrapper.eq(Appointments::getEmployeeId, employeeId);
        }
        if (serviceId != null) {
            queryWrapper.eq(Appointments::getServiceId, serviceId);
        }
        if (status != null && !status.isEmpty()) {
            queryWrapper.eq(Appointments::getStatus, status);
        }
        return RestBean.success(appointmentsService.list(queryWrapper));
    }

    // 分页查询预约
    @GetMapping("/page")
    public RestBean<Page<Appointments>> getAppointmentsByPage(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam(required = false) Integer customerId,
            @RequestParam(required = false) Integer employeeId,
            @RequestParam(required = false) Integer serviceId,
            @RequestParam(required = false) String status) {
        Page<Appointments> page = new Page<>(current, size);
        LambdaQueryWrapper<Appointments> queryWrapper = new LambdaQueryWrapper<>();
        if (customerId != null) {
            queryWrapper.eq(Appointments::getCustomerId, customerId);
        }
        if (employeeId != null) {
            queryWrapper.eq(Appointments::getEmployeeId, employeeId);
        }
        if (serviceId != null) {
            queryWrapper.eq(Appointments::getServiceId, serviceId);
        }
        if (status != null && !status.isEmpty()) {
            queryWrapper.eq(Appointments::getStatus, status);
        }
        return RestBean.success(appointmentsService.page(page, queryWrapper));
    }

    // 新增预约
    @PostMapping
    public RestBean<Boolean> addAppointment(@RequestBody Appointments appointment) {
        return RestBean.success(appointmentsService.save(appointment));
    }

    // 更新预约信息
    @PutMapping("/{id}")
    public RestBean<Boolean> updateAppointment(
            @PathVariable Integer id,
            @RequestBody Appointments appointment) {
        appointment.setId(id);
        return RestBean.success(appointmentsService.updateById(appointment));
    }

    // 删除预约
    @DeleteMapping("/{id}")
    public RestBean<Boolean> deleteAppointment(@PathVariable Integer id) {
        return RestBean.success(appointmentsService.removeById(id));
    }
}
